#include "ProxyServerToGameModule.h"
#include "ProxyServerNetClientPlugin.h"

bool FragrantGrass::ProxyServerToGameModule::Init()
{
	m_pNetClientModule = m_pPluginManager->FindModule<INetClientModule>();
	m_pProxyServerNet_ServerModule = m_pPluginManager->FindModule<IProxyServerNetServerModule>();
	m_pLogModule = m_pPluginManager->FindModule<ILogModule>();
	m_pConfigModule = m_pPluginManager->FindModule<IConfigModule>();

    return true;
}

bool FragrantGrass::ProxyServerToGameModule::Shut()
{
    //Final();
    //Clear();
    return true;
}

bool FragrantGrass::ProxyServerToGameModule::Execute()
{
	return true;
}

bool FragrantGrass::ProxyServerToGameModule::AfterInit()
{
	m_pNetClientModule->AddReceiveCallBack(FG_SERVER_TYPES::FG_ST_GAME, Protocol::EGMI_ACK_ENTER_GAME, this, &ProxyServerToGameModule::OnAckEnterGame);
	m_pNetClientModule->AddReceiveCallBack(FG_SERVER_TYPES::FG_ST_GAME, this, &ProxyServerToGameModule::Transpond);
	m_pNetClientModule->AddEventCallBack(FG_SERVER_TYPES::FG_ST_GAME, this, &ProxyServerToGameModule::OnSocketGSEvent);
	m_pNetClientModule->ExpandBufferSize();

    return true;
}

void FragrantGrass::ProxyServerToGameModule::OnSocketGSEvent(const SOCK nSockIndex, const FG_NET_EVENT eEvent, INet* pNet)
{
    if (eEvent & FG_NET_EVENT_EOF)
    {
    }
    else if (eEvent & FG_NET_EVENT_ERROR)
    {
    }
    else if (eEvent & FG_NET_EVENT_TIMEOUT)
    {
    }
    else  if (eEvent & FG_NET_EVENT_CONNECTED)
    {
        m_pLogModule->LogNormal(ILogModule::LL_INFO_NORMAL, GUID(0, nSockIndex), "FG_NET_EVENT_CONNECTED", "connected success", __FUNCTION__, __LINE__);
        Register(pNet);
    }
}

void FragrantGrass::ProxyServerToGameModule::Register(INet* pNet)
{
	const Protocol::ServerInfoReportList& serverInfoList = *(const Protocol::ServerInfoReportList*)m_pConfigModule->GetConfigDatas(IConfigModule::FileParseType::FPT_SERVER_DATA);
	int n = serverInfoList.server_list_size();
	for (int i = 0; i < n; ++i)
	{
		const Protocol::ServerInfoReport& serverInfo = serverInfoList.server_list(i);
		const int nServerType = serverInfo.server_type();
		const int nServerID = serverInfo.server_id();
		if (nServerType == FG_SERVER_TYPES::FG_ST_PROXY && m_pPluginManager->GetAppID() == nServerID)
		{
			Protocol::ServerInfoReportList xMsg;
			Protocol::ServerInfoReport* pData = xMsg.add_server_list();
			(*pData) = serverInfo;

			FG_SHARE_PTR<ConnectData> pServerData = m_pNetClientModule->GetServerNetInfo(pNet);
			if (pServerData)
			{
				int nTargetID = pServerData->nGameID;
				m_pNetClientModule->SendToServerByPB(nTargetID, Protocol::EGameMsgID::EGMI_PTWG_PROXY_REGISTERED, xMsg);

				m_pLogModule->LogNormal(ILogModule::LL_INFO_NORMAL, GUID(0, pData->server_id()), pData->server_name(), "Register");
			}
		}
	}
}

void FragrantGrass::ProxyServerToGameModule::OnAckEnterGame(const SOCK nSockIndex, const int nMsgID, const char* msg, const uint32_t nLen)
{
 //   GUID nPlayerID;
 //   NFMsg::AckEventResult xData;
 //   if (!NFINetModule::ReceivePB( nMsgID, msg, nLen, xData, nPlayerID))
 //   {
 //       return;
 //   }
 //
	//const GUID& xClient = NFINetModule::PBToNF(xData.event_client());
	//const GUID& xPlayer = NFINetModule::PBToNF(xData.event_object());

	//m_pProxyServerNet_ServerModule->EnterGameSuccessEvent(xClient, xPlayer);
	//m_pProxyServerNet_ServerModule->Transpond(nSockIndex, nMsgID, msg, nLen);
}

void FragrantGrass::ProxyServerToGameModule::LogServerInfo(const std::string& strServerInfo)
{
    m_pLogModule->LogNormal(ILogModule::LL_INFO_NORMAL, GUID(), strServerInfo, "");
}

void FragrantGrass::ProxyServerToGameModule::Transpond(const SOCK nSockIndex, const int nMsgID, const char * msg, const uint32_t nLen)
{
	m_pProxyServerNet_ServerModule->Transpond(nSockIndex, nMsgID, msg, nLen);
}
